﻿using UnityEngine;
using LuaInterface;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace LuaFramework {
    public class LuaBehaviour : View {
        private string data = null;
        private Dictionary<string, LuaFunction> buttons = new Dictionary<string, LuaFunction>();
        private Dictionary<string, LuaFunction> toggles = new Dictionary<string, LuaFunction>();
        private Dictionary<string, LuaFunction> sliders = new Dictionary<string, LuaFunction>();
        //private Dictionary<string, LuaFunction> pointerDown = new Dictionary<string, LuaFunction>();
        //private Dictionary<string, LuaFunction> pointerUp = new Dictionary<string, LuaFunction>();
        //private Dictionary<string, LuaFunction> pointerEnter = new Dictionary<string, LuaFunction>();
        //private Dictionary<string, LuaFunction> pointerExit = new Dictionary<string, LuaFunction>();

        protected void Awake() {
            Util.CallMethod(name, "Awake", gameObject);
        }

        protected void Start() {
            Util.CallMethod(name, "Start");
        }

        //protected void OnClick() {
        //    Util.CallMethod(name, "OnClick");
        //}

        //protected void OnClickEvent(GameObject go) {
        //    Util.CallMethod(name, "OnClick", go);
        //}

        //public void AddPointerDown(GameObject go, LuaFunction luafunc)
        //{
        //    if (go == null || luafunc == null) return;
        //    pointerDown.Add(go.name, luafunc);
        //    go.GetComponent<UIEventTrigger>().onPointerDown.AddListener(
        //        delegate (PointerEventData data)
        //        {
        //            luafunc.Call(go);
        //        }
        //    );
        //}
        //public void AddPointerUp(GameObject go, LuaFunction luafunc)
        //{
        //    if (go == null || luafunc == null) return;
        //    pointerUp.Add(go.name, luafunc);
        //    go.GetComponent<UIEventTrigger>().onPointerDown.AddListener(
        //        delegate (PointerEventData data)
        //        {
        //            luafunc.Call(go);
        //        }
        //    );
        //}
        //public void AddPointerEnter(GameObject go, LuaFunction luafunc)
        //{
        //    if (go == null || luafunc == null) return;
        //    pointerEnter.Add(go.name, luafunc);
        //    go.GetComponent<UIEventTrigger>().onPointerDown.AddListener(
        //        delegate (PointerEventData data)
        //        {
        //            luafunc.Call(go);
        //        }
        //    );
        //}
        //public void AddPointerExit(GameObject go, LuaFunction luafunc)
        //{
        //    if (go == null || luafunc == null) return;
        //    pointerExit.Add(go.name, luafunc);
        //    go.GetComponent<UIEventTrigger>().onPointerDown.AddListener(
        //        delegate (PointerEventData data)
        //        {
        //            luafunc.Call(go);
        //        }
        //    );
        //}
        /// <summary>
        /// 添加单击事件
        /// </summary>
        public void AddClick(GameObject go, LuaFunction luafunc) {
            if (go == null || luafunc == null) return;
            buttons.Add(go.name, luafunc);
            go.GetComponent<Button>().onClick.AddListener(
                delegate() {
                    luafunc.Call(go);
                }
            );
        }
        /// <summary>
        /// 添加Toggle点击事件
        /// </summary>
        /// <param name="go"></param>
        /// <param name="luafunc"></param>
        public void AddToggleClick(GameObject go, LuaFunction luafunc)
        {
            if (go == null || luafunc == null) return;
            toggles.Add(go.name, luafunc);
            go.GetComponent<Toggle>().onValueChanged.AddListener(
                delegate (bool value) {
                    luafunc.Call(go,value);
                }
            );
        }
        /// <summary>
        /// 添加滑条事件
        /// </summary>
        /// <param name="go"></param>
        /// <param name="laufunc"></param>
        public void AddSliderEvent(GameObject go, LuaFunction luafunc)
        {
            if (go == null || luafunc == null) return;
            sliders.Add(go.name, luafunc);
            go.GetComponent<Slider>().onValueChanged.AddListener(
                delegate (float value)
                {
                    luafunc.Call(go, value);
                });
        }
        /// <summary>
        /// 删除单击事件
        /// </summary>
        /// <param name="go"></param>
        public void RemoveClick(GameObject go)
        {
            if (go == null) return;
            LuaFunction luafunc = null;
            if (buttons.TryGetValue(go.name, out luafunc))
            {
                luafunc.Dispose();
                luafunc = null;
                buttons.Remove(go.name);
            }
            if (toggles.TryGetValue(go.name, out luafunc))
            {
                luafunc.Dispose();
                luafunc = null;
                toggles.Remove(go.name);
            }
            if (sliders.TryGetValue(go.name, out luafunc))
            {
                luafunc.Dispose();
                luafunc = null;
                sliders.Remove(go.name);
            }
            //if (pointerDown.TryGetValue(go.name, out luafunc))
            //{
            //    luafunc.Dispose();
            //    luafunc = null;
            //    pointerDown.Remove(go.name);
            //}
            //if (pointerUp.TryGetValue(go.name, out luafunc))
            //{
            //    luafunc.Dispose();
            //    luafunc = null;
            //    pointerUp.Remove(go.name);
            //}
            //if (pointerEnter.TryGetValue(go.name, out luafunc))
            //{
            //    luafunc.Dispose();
            //    luafunc = null;
            //    pointerEnter.Remove(go.name);
            //}
            //if (pointerExit.TryGetValue(go.name, out luafunc))
            //{
            //    luafunc.Dispose();
            //    luafunc = null;
            //    pointerExit.Remove(go.name);
            //}
        }

        /// <summary>
        /// 清除单击事件
        /// </summary>
        public void ClearClick()
        {
            foreach (var de in buttons)
                if (de.Value != null)
                    de.Value.Dispose();
            foreach (var de in toggles)
                if (de.Value != null)
                    de.Value.Dispose();
            foreach (var de in sliders)
                if (de.Value != null)
                    de.Value.Dispose();
            //foreach (var de in pointerDown)
            //    if (de.Value != null)
            //        de.Value.Dispose();
            //foreach (var de in pointerUp)
            //    if (de.Value != null)
            //        de.Value.Dispose();
            //foreach (var de in pointerEnter)
            //    if (de.Value != null)
            //        de.Value.Dispose();
            //foreach (var de in pointerExit)
            //    if (de.Value != null)
            //        de.Value.Dispose();
            buttons.Clear();
            toggles.Clear();
            sliders.Clear();
            //pointerDown.Clear();
            //pointerUp.Clear();
            //pointerEnter.Clear();
            //pointerExit.Clear();
        }
        //-----------------------------------------------------------------
        protected void OnDestroy() {
            ClearClick();
#if ASYNC_MODE
            //string abName = name.ToLower().Replace("panel", "");
            //ResManager.UnloadAssetBundle(abName + AppConst.ExtName);
#endif
            Util.ClearMemory();
            Util.CallMethod(name, "OnDestroy");
            Debug.Log("~" + name + " was destroy!");
        }
    }
}